package api

import (
	"ams/dao"
	"ams/global"
	"ams/service"
	"fmt"
	"github.com/gin-gonic/gin"
	"lib/http"
	"lib/id"
	"lib/structs/ams"
	"lib/structs/enum"
	"lib/structs/tables"
	"path/filepath"
	"strings"
)

var AssetApi = new(assetApi)

type assetApi struct {
}

func (a assetApi) TypeList(c *gin.Context) {
	li, err := dao.AssetDao.FindTypeList()
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.Ok(c, li)
}

func (a assetApi) CreateType(c *gin.Context) {
	var in tables.AmsAssetType
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	in.Id = id.GetSnowId()
	err := dao.AssetDao.CreateType(in)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}
func (a assetApi) DeleteType(c *gin.Context) {
	in := struct {
		Id int64 `form:"id" binding:"required"`
	}{}
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	if in.Id < 100 {
		err := fmt.Errorf("系统默认数据，不能删除！")
		http.BadWithErr(c, err)
		return
	}
	num, err := dao.AssetDao.CountByType(in.Id)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	if num > 0 {
		err = fmt.Errorf("该类型下还有资产记录，不能删除！")
		http.BadWithErr(c, err)
		return
	}
	err = dao.AssetDao.DeleteType(in.Id)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (a assetApi) CategoryList(c *gin.Context) {
	li, err := dao.AssetDao.FindCategoryList()
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.Ok(c, li)
}

func (a assetApi) CategoryTree(c *gin.Context) {
	li, err := dao.AssetDao.FindCategoryList()
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	nodes := make([]ams.AssetCategoryNode, len(li))
	for i, item := range li {
		nodes[i] = ams.AssetCategoryNode{
			Value:       item.Id,
			Label:       item.Name,
			Description: item.Description,
			ParentId:    item.ParentId,
		}
	}
	tree := service.AssetService.AssetCategoryNode(0, nodes)
	http.Ok(c, tree)
}

func (a assetApi) CreateCategory(c *gin.Context) {
	var in tables.AmsAssetCategory
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	in.Id = id.GetSnowId()
	err := dao.AssetDao.CreateCategory(in)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}
func (a assetApi) DeleteCategory(c *gin.Context) {
	in := struct {
		Id int64 `form:"id" binding:"required"`
	}{}
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	if in.Id < 100 {
		err := fmt.Errorf("系统默认数据，不能删除！")
		http.BadWithErr(c, err)
		return
	}
	num, err := dao.AssetDao.CountByCategory(in.Id)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	if num > 0 {
		err = fmt.Errorf("该类别下还有资产记录，不能删除！")
		http.BadWithErr(c, err)
		return
	}
	err = dao.AssetDao.DeleteCategory(in.Id)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (a assetApi) Page(c *gin.Context) {
	var in ams.SearchAssetDto
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	li, total, err := service.AssetService.Page(in)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OkPage(c, li, total)
}

func (a assetApi) Add(c *gin.Context) {
	var in ams.AddAssetDto
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	err := service.AssetService.Add(in)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (a assetApi) Del(c *gin.Context) {
	in := struct {
		Id int64 `form:"id,string" binding:"required"`
	}{}
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	err := service.AssetService.DeleteById(in.Id)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (a assetApi) AssetConsLogsPage(c *gin.Context) {
	var in ams.SearchAssetConsDto
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	li, total, err := service.AssetService.AssetConsLogsPage(in)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OkPage(c, li, total)
}
func (a assetApi) AssetConsPage(c *gin.Context) {
	var in ams.SearchAssetDto
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	li, total, err := service.AssetService.AssetConsPage(in)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OkPage(c, li, total)
}
func (a assetApi) AssetConsList(c *gin.Context) {
	li, err := dao.AssetDao.AssetConsList()
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	for i, item := range li {
		li[i].Value = fmt.Sprintf("%d", item.AssetId)
		li[i].Label = item.Name
	}
	http.Ok(c, li)
}

func (a assetApi) AssetConsCreateLogs(c *gin.Context) {
	var in tables.AmsAssetConsLog
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	err := service.AssetService.AddAssetConsLogs(in)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

// UploadOrderPic 上传单据图片
func (a assetApi) UploadOrderPic(c *gin.Context) {
	f, err := c.FormFile("file")
	if err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	suf := f.Filename[strings.LastIndex(f.Filename, "."):]
	filename := fmt.Sprintf("%d%s", id.GetSnowId(), suf)
	fPath := filepath.Join(global.Conf.AppFolder, "orderPic", filename)
	err = c.SaveUploadedFile(f, fPath)
	if err != nil {
		http.BadWithCode(c, enum.ErrorMap.UploadFileErrForIdp)
		return
	}
	http.Ok(c, gin.H{"filename": filename})
}

func (a assetApi) CallOCR(c *gin.Context) {
	//todo https://cloud.tencent.com/act/pro/ocr61
	http.BadWithErr(c, fmt.Errorf("该功能需要 腾讯OCR 服务支持"))
}
